/*
 * @Description: 
 * @Author: yyh
 * @Date: 2022-03-31 23:41:00
 * @LastEditors: yyh
 * @LastEditTime: 2022-05-16 14:16:27
 */
var db = require("../utils/db");
var url = require("url");
var qs = require("querystring");

//新增报废资产数据
addScrapInfo = async (req, res) => {
    var {
      asset_id,
      asset_name,
      scrap_time,
      status,
      scrap_reason,
      operator
    } = req.body;
    var sql =
      "insert into scrap (asset_id,asset_name,scrap_time,status,scrap_reason,operator) values(?,?,?,?,?,?)";
    var sqlArr = [
      asset_id,
      asset_name,
      scrap_time,
      status,
      scrap_reason,
      operator
    ];
    var callBack = async (err, data) => {
      if(err) {
          console.log(err);
          res.send({
              code: 400,
              msg: "新增失败"
          })
      } else {
          res.send({
              code: 200,
              msg: "新增成功",
              data: data
          })
      }
  }
  db.sqlConnect(sql,sqlArr,callBack);
  };

  //删除报废资产数据
  deleteScrap = async (req, res) => {
    var id = req.query[0];
    var sql = "delete from scrap where id = ?";
    var sqlArr = [id];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
      res.send({
        code: 200,
        msg: "删除成功"
      });
    } else {
      res.send({
        code: 400,
        msg: "删除失败"
      });
    }
  };

  //修改报废资产数据
  updateScrapInfo = async (req, res) => {
    var {
      id,
      asset_id,
      asset_name,
      scrap_time,
      scrap_reason,
      operator
    } = req.body;
    var sql =
      "update scrap set asset_id=?,asset_name=?,scrap_time=?,scrap_reason=?,operator=? where id=?";
    var sqlArr = [
        asset_id,
        asset_name,
        scrap_time,
        scrap_reason,
        operator,
        id
    ];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
      res.send({
        code: 200,
        msg: "修改成功"
      });
    } else {
      res.send({
        code: 400,
        msg: "修改失败"
      });
    }
  };

  //通过id修改报废资产状态
  updateScrapStatus = async (req, res) => {
    var {
      id,
      status
    } = req.body;
    var sql =
      "update scrap set status=? where id=?";
    var sqlArr = [
      status,
      id
    ];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
      res.send({
        code: 200,
        msg: "修改成功"
      });
    } else {
      res.send({
        code: 400,
        msg: "修改失败"
      });
    }
  };

  //模糊查询、分页查询报废资产数据
  getScrapInfo = async (req, res) => {
    var params = url.parse(req.url).query;
    params = qs.parse(params);
    var sql = "select * from scrap";
    var sqlArr = [];
    var isMore = false; //是否有多个查询参数
    if(params.asset_id) {
        sql += " where asset_id like ?";
        sqlArr.push("%" + params.asset_id + "%");
        isMore = true;
    }
    if (params.status) {
      if (isMore) {
        sql += " and status like ?";
      } else {
        sql += " where status like ?";
        isMore = true;
      }
      sqlArr.push("%" + params.status + "%");
    }
    if (params.startTime && params.endTime) {
      if (isMore) {
        sql += " and scrap_time between ? and ?";
      } else {
        sql += " where scrap_time between ? and ?";
      }
      sqlArr.push(params.startTime, params.endTime);
    }
    if(params.page && params.pageSize) {
        sql += " limit ?,?";
        sqlArr.push((params.page -1)* params.pageSize, parseInt(params.pageSize));
    }
    var callBack = async (err, data) => {
        if(err) {
            console.log(err);
            res.send({
                code: 400,
                msg: "模糊查询失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "模糊查询成功",
                data: data
            })
        }
    }
    db.sqlConnect(sql,sqlArr,callBack);
}

//通过id查询报废资产数据
getScrapInfoById = async (req,res) => {
  var id = req.query[0];
  var sql = "select * from scrap where id=?";
  var sqlArr = [id];
  var callBack = async (err, data) => {
    if(err) {
        console.log(err);
        res.send({
            code: 400,
            msg: "查询失败"
        })
    } else {
        res.send({
            code: 200,
            msg: "查询成功",
            data: data
        })
    }
}
db.sqlConnect(sql,sqlArr,callBack);
}

  module.exports = {
    addScrapInfo,
    deleteScrap,
    updateScrapInfo,
    updateScrapStatus,
    getScrapInfo,
    getScrapInfoById
  };